package cn.jly.flink.sideoutput;

import cn.jly.flink.entity.MetricEvent;
import cn.jly.flink.utils.ConstantUtils;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.RandomUtils;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;

/**
 * @PackageName cn.jly.flink.sideoutput
 * @ClassName InsertData2Redis
 * @Description
 * @Author 姬岚洋
 * @Date 2021/1/16 下午 1:26
 */
public class InsertData2Redis {
    public static void main(String[] args) {
        String ip = "192.168.172.101";
        final HashSet<HostAndPort> set = new HashSet<>(6);
        set.add(new HostAndPort(ip, 7001));
        set.add(new HostAndPort(ip, 7002));
        set.add(new HostAndPort(ip, 7003));
        set.add(new HostAndPort(ip, 7004));
        set.add(new HostAndPort(ip, 7005));
        set.add(new HostAndPort(ip, 7006));

        final List<String> list = Arrays.asList("machine", "docker", "application", "middleware", "other");

        try (JedisCluster jedisCluster = new JedisCluster(set)) {
            for (int i = 0; i < 100; i++) {
                final MetricEvent metricEvent = new MetricEvent();
                metricEvent.setTimestamp(System.currentTimeMillis());
                metricEvent.setEventDesc(UUID.randomUUID().toString());
                metricEvent.setEventTag(list.get(RandomUtils.nextInt(0, list.size())));
                metricEvent.setId(ConstantUtils.ID_GENERATOR.getAndIncrement());

                // 存入rbb
                jedisCluster.hset(metricEvent.getClass().getSimpleName(), String.valueOf(metricEvent.getId()), JSON.toJSONString(metricEvent));
            }
            System.out.println("插入成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
